import numpy as np
from keras.callbacks import Callback


class EarlyStopping(Callback):

    def __init__(self):
        super().__init__()

    def on_train_begin(self, logs=None):
        with open("stop_training.txt", 'w') as f:
            f.write('')

    def on_epoch_end(self, epoch, logs=None):
        with open("stop_training.txt", 'r') as f:
            data = f.read()
            if data != '':  # 终止信号
                self.model.stop_training = True  # 提前停止训练的关键代码
